package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.rooms.api.home.vo.RoomLayoutVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.grounding.vo.GroundingVo;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.pricing.vo.RoomHousePricingPageResp;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice.vo.RoomDevicePageVO;
import com.xiaoshuidi.cloud.module.rooms.enums.RentStatusEnum;
import com.xiaoshuidi.cloud.module.rooms.enums.RoomHouseFilingsStatus;
import com.xiaoshuidi.cloud.module.rooms.enums.RoomTypeEnum;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomHouseMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouse;
import com.xiaoshuidi.cloud.module.rooms.pojo.dto.roomhouse.HouseDisperseFilingsDTO;
import com.xiaoshuidi.cloud.module.rooms.pojo.dto.roomhouse.RoomHouseFilingsListDTO;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class RoomHouseRepository extends ServiceImpl<RoomHouseMapper, RoomHouse> {
    private final RoomHouseMapper roomHouseMapper;

    public CommonResult<Boolean> insertBatch(List<RoomHouse> roomHouses) {
        roomHouseMapper.insertBatch(roomHouses);
        return CommonResult.success(true);
    }

    public CommonResult<List<RoomLayoutVO>> queryRoomLayout() {
        List<RoomLayoutVO> roomLayoutVOS = roomHouseMapper.queryRoomLayout();
        return CommonResult.success(roomLayoutVOS);
    }

    /**
     * 根据楼层和上级查询所有房间
     *
     * @param floorId
     * @param superId
     * @return
     */
    public List<RoomHouse> getByFloorIdAndSuperId(Long floorId, Long superId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getFloorId, floorId)
                .eq(RoomHouse::getSuperId, superId)
                .orderByDesc(RoomHouse::getCreateTime);
        return roomHouseMapper.selectList(wrapper);
    }

    public RoomHouse queryByWrapper(LambdaQueryWrapper<RoomHouse> wrapper) {
        return roomHouseMapper.selectOne(wrapper);
    }

    /**
     * 根据公寓查询所有房间
     *
     * @param apartmentId
     * @return
     */
    public List<RoomHouse> getByApartmentId(Long apartmentId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getApartmentId, apartmentId);
        return roomHouseMapper.selectList(wrapper);
    }

    /**
     * 删除公寓下的所有房间
     *
     * @param apartmentId
     */
    public CommonResult<Boolean> delByApartmentId(Long apartmentId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getApartmentId, apartmentId);
        roomHouseMapper.delete(wrapper);
        return CommonResult.success(Boolean.TRUE);
    }

    /**
     * 删除公寓下的所有房间
     *
     * @param floorId
     */
    public CommonResult<Boolean> delByFloorId(@NonNull Long floorId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getFloorId, floorId);
        roomHouseMapper.delete(wrapper);
        return CommonResult.success(Boolean.TRUE);
    }

    public CommonResult<List<RoomHouse>> getBySuperId(Long roomHouseId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getSuperId, roomHouseId);
        roomHouseMapper.selectList(wrapper);
        return CommonResult.success(roomHouseMapper.selectList(wrapper));
    }

    /**
     * 根据公寓ID、房间号查询房型ID
     *
     * @param apartmentId
     * @param houseNo
     * @return
     */
    public List<Long> distinctLayoutId(Long apartmentId, String houseNo) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(apartmentId != null, RoomHouse::getApartmentId, apartmentId)
                .eq(RoomHouse::getIsConcentrated, Boolean.TRUE)
                .like(RoomHouse::getName, houseNo);
        List<RoomHouse> roomHouses = roomHouseMapper.distinctLayoutId(wrapper);
        if (CollectionUtil.isNotEmpty(roomHouses)) {
            List<Long> layoutIds = roomHouses.stream().map(s -> s.getLayoutId()).collect(Collectors.toList());
            return layoutIds;
        }
        return null;
    }

    /**
     * 根据公寓ID、房型ID获取所有房源
     *
     * @param apartmentId
     * @param layoutId
     * @return
     */
    public List<RoomHouse> listByApartmentIdAndLayoutId(Long apartmentId, Long layoutId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getApartmentId, apartmentId)
                .eq(RoomHouse::getLayoutId, layoutId);
        return list(wrapper);
    }

    /**
     * 根据公寓ID、房型ID、上下架状态、房间号获取所有房源
     *
     * @param groundingVo
     * @return
     */
    public IPage<GroundingVo> getGroundingRoomList(GroundingVo groundingVo) {
        QueryWrapper<RoomHouse> queryWrapper = Wrappers.query();
        queryWrapper.eq(groundingVo.getApartmentId() != null,"gur.apartmentId", groundingVo.getApartmentId())
                .eq(groundingVo.getRoomHouseTypeId()!= null, "gur.roomHouseTypeId",groundingVo.getRoomHouseTypeId());
                if(StrUtil.isNotBlank(groundingVo.getIsGrounding())){
                    queryWrapper.eq("gur.isGrounding",Boolean.parseBoolean(groundingVo.getIsGrounding()));
                }
            queryWrapper.like("gur.roomName",groundingVo.getHouseNo());
        Page<GroundingVo> page = Page.of(groundingVo.getPageNo(), groundingVo.getPageSize());
        page=this.baseMapper.getGroundingRoomList(queryWrapper,page);
        return page;
    }

    /**
     * 根据房间名和上级房源id查询某一房间
     *
     * @param roomName
     * @param supperHouseId
     * @return
     */
    public RoomHouse getByRoomNameIdAndSuperId(String roomName, Long supperHouseId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getName, roomName);
        wrapper.eq(RoomHouse::getSuperId, supperHouseId);
        return getOne(wrapper);
    }

    /**
     * 房源定价分页列表
     *
     * @param apartmentId
     * @param layoutId
     * @param houseNo
     * @param pageSize
     * @param pageNo
     * @return
     */
    public Page<RoomHousePricingPageResp> pricingPage(Long apartmentId, String layoutId, String houseNo, Boolean isConcentrated, Integer pageSize, Integer pageNo) {
        QueryWrapper<RoomHouse> wrapper = Wrappers.query();
        wrapper.eq(apartmentId != null, "rh.apartment_id", apartmentId)
                .eq(layoutId != null && isConcentrated, "rh.layout_id", layoutId)
                .eq("rh.is_concentrated", isConcentrated)
                .and(!isConcentrated, s -> s.eq("rh.is_whole", true)
                        .or(v -> v.eq("rh.is_whole", false)
                                .isNotNull("rh.super_id")
                                .ne("rh.room_type", RoomTypeEnum.GGQY.getValue())))
                .like(StrUtil.isNotEmpty(houseNo), "rh.house_no", houseNo)
                .eq("rh.deleted", false)
                .orderByAsc("rh.area")
                .orderByAsc("rh.building_no")
                .orderByAsc("rh.house_no")
                .orderByDesc("rh.id");
        Page<RoomHousePricingPageResp> page = Page.of(pageNo, pageSize);
        page = this.baseMapper.pricingPage(wrapper, page);
        return page;
    }

    /**
     * 根据id查询房源,并验空
     *
     * @param roomHouseId
     * @return
     */
    public RoomHouse myGetById(@NonNull Long roomHouseId) {
        RoomHouse roomHouse = this.getById(roomHouseId);
        Assert.isTrue(ObjectUtil.isNotEmpty(roomHouse), "房源信息不存在");
        return roomHouse;
    }

    /**
     * 房源定价导出count
     *
     * @param apartmentId
     * @param layoutId
     * @param houseNo
     * @param isConcentrated
     * @return
     */
    public long pricingCount(Long apartmentId, String layoutId, String houseNo, Boolean isConcentrated) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(apartmentId != null, RoomHouse::getApartmentId, apartmentId)
                .eq(layoutId != null && isConcentrated, RoomHouse::getLayoutId, layoutId)
                .eq(RoomHouse::getIsConcentrated, isConcentrated)
                .and(!isConcentrated, s -> s.eq(RoomHouse::getIsWhole, true)
                        .or(v -> v.eq(RoomHouse::getIsWhole, false)
                                .isNotNull(RoomHouse::getSuperId)
                                .ne(RoomHouse::getRoomType, RoomTypeEnum.GGQY.getValue())))
                .eq(StrUtil.isNotEmpty(houseNo), RoomHouse::getHouseNo, houseNo);
        return count(wrapper);
    }

    /**
     * 根据编号集合查询房源
     *
     * @param noList
     * @return
     */
    public List<RoomHouse> listByNos(Long apartmentId, Boolean isConcentrated, List<String> noList) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.select(RoomHouse::getId, RoomHouse::getNo, RoomHouse::getMonthRental)
                .eq(RoomHouse::getApartmentId, apartmentId)
                .eq(RoomHouse::getIsConcentrated, isConcentrated)
                .in(RoomHouse::getNo, noList);
        return list(wrapper);
    }

    public List<RoomHouse> getByLayoutId(@NonNull Long layoutId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getLayoutId, layoutId);
        return list(wrapper);
    }

    /**
     * 获取未备案的集中式房源信息
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<RoomHouseFilingsListDTO> filingsHouseList(LocalDateTime startTime, LocalDateTime endTime) {
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.eq("rh.deleted", false)
                .eq("rh.is_concentrated", true)
                .ge("rh.update_time", startTime)
                .lt(endTime != null, "rh.update_time", endTime)
                .and(s -> s.ne("rhf.status", RoomHouseFilingsStatus.SUCCESS.getCode()).or().isNull("rhf.status"));
        return this.baseMapper.filingsHouseList(wrapper);
    }

    /**
     * 获取分散式备案的房源信息
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<HouseDisperseFilingsDTO> disperseFilingsList(LocalDateTime startTime, LocalDateTime endTime) {
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.eq("rh.deleted", false)
                .eq("rh.is_concentrated", false)
                .ge("rh.update_time", startTime)
                .lt(endTime != null, "rh.update_time", endTime)
                .and(s -> s.ne("rhf.status", RoomHouseFilingsStatus.SUCCESS.getCode()).or().isNull("rhf.status"));
        return this.baseMapper.disperseFilingsList(wrapper);
    }

    /**
     * 查询门店下的所有房间, 按价格倒序
     * 7.0.5 房间限制为空房未锁定
     * @param apartmentId
     * @return
     */
    public List<RoomHouse> getByApartmentIdOrderByMonthRental(@NonNull Long apartmentId) {
        QueryWrapper<RoomHouse> wrapper = Wrappers.query();
        wrapper.eq("rsr.is_current_state", true)
                .eq("rsr.state", RentStatusEnum.EMPTY.getValue())
                .eq("rh.apartment_id", apartmentId)
                .isNotNull("rh.month_rental")
                .eq("rh.is_lock", false)
                .orderByAsc("rh.month_rental");
        return roomHouseMapper.getByApartmentIdOrderByMonthRental(wrapper);
    }

    /**
     * 查询户型下的所有房间, 按价格倒序
     *
     * @param layoutId
     * @return
     */
    public List<RoomHouse> getByLayoutIdOrderByMonthRental(@NonNull Long layoutId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getLayoutId, layoutId);
        wrapper.isNotNull(RoomHouse::getMonthRental);
        wrapper.orderByAsc(RoomHouse::getMonthRental);
        return this.list(wrapper);
    }

    public List<RoomHouse> getByIdAndSuperIdOrderByMonthRental(@NonNull Long roomHouseId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getId, roomHouseId);
        wrapper.isNotNull(RoomHouse::getMonthRental);
        wrapper.or(wrap -> wrap.eq(RoomHouse::getSuperId, roomHouseId));
        wrapper.orderByAsc(RoomHouse::getMonthRental);
        return this.list(wrapper);
    }

    public List<RoomHouse> getRoomList(List<Long> ids) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();

        wrapper.select(RoomHouse::getId, RoomHouse::getName, RoomHouse::getFloorId, RoomHouse::getFloorNo)
                .in(RoomHouse::getApartmentAreaId, ids);

        return roomHouseMapper.selectList(wrapper);
    }

    /**
     * 根据房源小区id和楼层id查询房源
     *
     * @param apartmentAreaId 房源小区id
     * @param floorId         楼层id
     * @return
     */
    public List<RoomHouse> getByApartmentAreaIdAndFloorId(Long apartmentAreaId, Long floorId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getApartmentAreaId, apartmentAreaId);
        wrapper.eq(RoomHouse::getFloorId, floorId);
        wrapper.eq(RoomHouse::getIsConcentrated, true);
        return this.list(wrapper);
    }

    public List<RoomHouse> getByRoomLayoutId(Long layoutId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getLayoutId, layoutId);
        return this.list(wrapper);
    }

    public List<RoomHouse> getByIsConcentratedGroupByAddress(Boolean isConcentrated) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getIsConcentrated, isConcentrated);
        wrapper.groupBy(RoomHouse::getProvince, RoomHouse::getCity, RoomHouse::getDistrict, RoomHouse::getStreet, RoomHouse::getAddress, RoomHouse::getArea);
        return this.list(wrapper);
    }

    public RoomHouse getByApartmentAreaIdAndName(Long apartmentAreaId, String houseNo) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getApartmentAreaId, apartmentAreaId);
        wrapper.eq(RoomHouse::getHouseNo, houseNo);
        return getOne(wrapper);
    }

    public RoomHouse getByAddressAndHouseNo(String province, String city, String district, String street, String address, String areaName, String buildingNo, String unitNo, String houseNo) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getProvince, province);
        wrapper.eq(RoomHouse::getCity, city);
        wrapper.eq(RoomHouse::getDistrict, district);
        wrapper.eq(RoomHouse::getStreet, street);
        wrapper.eq(RoomHouse::getAddress, address);
        wrapper.eq(RoomHouse::getArea, areaName);
        wrapper.eq(RoomHouse::getBuildingNo, buildingNo);
        wrapper.eq(StrUtil.isNotEmpty(unitNo), RoomHouse::getUnitNo, unitNo);
        wrapper.eq(RoomHouse::getHouseNo, houseNo);
        return getOne(wrapper);
    }

    public List<RoomHouse> getByApartmentIdAndLayoutIdOrderByMonthRental(Long apartmentId, Long layoutId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getApartmentId, apartmentId);
        wrapper.eq(RoomHouse::getLayoutId, layoutId);
        wrapper.isNotNull(RoomHouse::getMonthRental);
        wrapper.orderByAsc(RoomHouse::getMonthRental);
        return this.list(wrapper);
    }

    public List<RoomHouse> getEmptyRoomByLayoutId(Long layoutId) {
        QueryWrapper<RoomHouse> wrapper = Wrappers.query();
        wrapper.eq("p2.is_current_state", true);
        wrapper.eq("p.layout_id", layoutId);
        wrapper.eq("p2.state", RentStatusEnum.EMPTY.getValue());
        return roomHouseMapper.getEmptyRoomByLayoutId(wrapper);
    }

    public Integer isHasEmptyRoom(Long layoutId) {
        QueryWrapper<RoomHouse> wrapper = Wrappers.query();
        wrapper.eq("p.deleted", false);
        wrapper.eq("p2.is_current_state", true);
        wrapper.eq("p.layout_id", layoutId);
        wrapper.eq("p.is_lock", 0);
        wrapper.eq("p.grounding",1);
        wrapper.eq("p2.state", RentStatusEnum.EMPTY.getValue());
        return roomHouseMapper.isHasEmptyRoom(wrapper);
    }

    /**
     * 检查小区下是否存在集中式房源
     *
     * @param roomAreaId
     * @param isConcentrated
     * @return
     */
    public List<RoomHouse> getByRoomAreaIdAndIsConcentrated(Long roomAreaId, boolean isConcentrated) {
        return roomHouseMapper.getByRoomAreaIdAndIsConcentrated(roomAreaId, isConcentrated);
    }


    /**
     * 设备绑定列表查询
     *
     * @param apartmentId
     * @param area
     * @param buildingNo
     * @param unitNo
     * @param houseNo
     * @param isConcentrated
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<RoomDevicePageVO> devicePage(Long apartmentId, String area, String buildingNo,
                                             String unitNo, String houseNo, Boolean isConcentrated, Integer pageNo, Integer pageSize) {
        Page<RoomDevicePageVO> page = Page.of(pageNo, pageSize);
        QueryWrapper<RoomHouse> wrapper = Wrappers.query();
        wrapper.eq("rh.is_concentrated", isConcentrated)
                .eq(apartmentId != null, "rh.apartment_id", apartmentId)
                .eq(StrUtil.isNotEmpty(area), "rh.area", area)
                .eq(StrUtil.isNotEmpty(buildingNo), "rh.building_no", buildingNo)
                .eq(StrUtil.isNotEmpty(unitNo), "rh.unit_no", unitNo)
                .like(StrUtil.isNotEmpty(houseNo), "rh.house_no", houseNo)
                .and(!isConcentrated, s -> s.eq("rh.is_whole", 1).or(k -> k.eq("rh.is_whole", 0).and(j ->
                        j.ne("rh.room_type", '4').or().isNull("rh.room_type"))));
        page = roomHouseMapper.devicePage(wrapper, page);
        return page;
    }

    /**
     * 查询房型下是否存在空房
     * @param roomLayoutIds
     * @return
     */
    public Map<Long, Boolean> isHasEmptyRoomByIds(List<Long> roomLayoutIds) {
        Map<Long, Boolean> map = new HashMap<>();
        QueryWrapper<RoomHouse> wrapper = Wrappers.query();
        wrapper.eq("p.deleted", false);
        wrapper.eq("p2.is_current_state", true);
        wrapper.eq("p.grounding", true);
        wrapper.in("p.layout_id", roomLayoutIds);
        wrapper.eq("p2.state", RentStatusEnum.EMPTY.getValue());
        List<RoomHouse> roomHouses = roomHouseMapper.isHasEmptyRoomByIds(wrapper);
        Map<Long, List<RoomHouse>> layoutRoomMap = roomHouses.stream().collect(Collectors.groupingBy(RoomHouse::getLayoutId));
        layoutRoomMap.forEach((id, roomHosues) ->{
            map.put(id, ObjectUtil.isEmpty(roomHosues));
        });
        return map;
    }

    public List<RoomHouse> selectListByIds(List<String> ids) {
        LambdaQueryWrapper<RoomHouse> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.in(RoomHouse::getId,ids);
        return roomHouseMapper.selectList(queryWrapper);
    }

    public CommonResult<Boolean> isCleanRoomHouseEdit(Long roomHouseId, Boolean isClean) {
        RoomHouse updateRoomHouse = new RoomHouse();
        updateRoomHouse.setIsClean(isClean);
        // 使用 QueryWrapper 来构建更新条件
        UpdateWrapper<RoomHouse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", roomHouseId);
        roomHouseMapper.update(updateRoomHouse, updateWrapper);
        return CommonResult.success(true);

    }

    public CommonResult<Boolean> setGroundingStatus(List roomIds,Boolean doGrounding) {
        UpdateWrapper<RoomHouse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("grounding",doGrounding).in("id", roomIds);
        roomHouseMapper.update(null, updateWrapper);
        return CommonResult.success(true);
    }

    public CommonResult<Boolean> setAllGrounding() {
        UpdateWrapper<RoomHouse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("grounding",true);
        roomHouseMapper.update(null, updateWrapper);
        return CommonResult.success(true);
    }

    public Long getGroundingNum() {
        return roomHouseMapper.getGroundingNum();
    }
    public Long getUnGroundingNum() {
        return roomHouseMapper.getUnGroundingNum();
    }

    public CommonResult<Boolean> isRepairRoomHouseEdit(Long roomHouseId, Boolean isRepair) {
        RoomHouse updateRoomHouse = new RoomHouse();
        updateRoomHouse.setIsRepair(isRepair);
        // 使用 QueryWrapper 来构建更新条件
        UpdateWrapper<RoomHouse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", roomHouseId);
        roomHouseMapper.update(updateRoomHouse, updateWrapper);
        return CommonResult.success(true);
    }

    public List<RoomHouse> selectAll(Long apartmentId) {
        LambdaQueryWrapper<RoomHouse> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomHouse::getApartmentId,apartmentId);
        List<RoomHouse> list = roomHouseMapper.selectList(queryWrapper);
        return list;
    }

    @TenantIgnore
    public RoomHouse getByRoomId(Long roomHouseID) {
        return roomHouseMapper.selectById(roomHouseID);
    }

    /**
     * 房型下的房源数量
     * @param layoutId
     * @return
     */
    public Long getRoomNumByRoomLayout(Long layoutId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getLayoutId, layoutId);
        wrapper.eq(RoomHouse::getGrounding,1);
        return roomHouseMapper.selectCount(wrapper);
    }

    /**
     * 获取上一次停用的子房间
     * @param superId
     * @return
     */
    public List<RoomHouse> getBySuperIdAndIsLastStop(Long superId) {
        LambdaQueryWrapper<RoomHouse> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouse::getSuperId, superId);
        wrapper.eq(RoomHouse::getIsLastStop, true);
        return roomHouseMapper.selectList(wrapper);
    }
}
